local skill = fk.CreateSkill{
  name = "tea__weibing",
}
local U = require "packages/utility/utility"
local countNumbers = function(cards)
  local n = 0 
  for _, id in ipairs(cards) do
    n = n + Fk:getCardById(id).number
  end
  return n
end
---@type TrigSkelSpec<AimFunc>
local weibing = {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skill.name) and player == target and data.card.is_damage_card and data.firstTarget
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = data:getAllTargets()
    if #targets > 0 then
      room:sortByAction(targets)
      local cids = room:getNCards(3)
      room:moveCardTo(cids, Card.Processing, nil, fk.ReasonJustMove)
      for _, id in ipairs(cids) do
        room:setCardMark(Fk:getCardById(id), "@@tea__weibing_mark-phase", 1)
      end
      for _, p in ipairs(targets) do
        if #cids < 1 then break end
        local suc, dat = room:askToUseActiveSkill(p, {
          skill_name = "tea__weibing_ask_skill",
          prompt = "#tea__weibing-invoke:" .. player.id .. "::" .. tostring(countNumbers(cids)) .. ":" .. data.card:toLogString(),
          extra_data = {
            hand_cards = p:getCardIds("h"), shown_cards = cids
          }
        })
        if suc and dat and dat.cards then
          local cards = dat.cards
          room:throwCard(cards, skill.name, p, p)
          data.use.nullifiedTargets = data.use.nullifiedTargets or {}
          table.insertIfNeed(data.use.nullifiedTargets, p)
        else
          if #cids > 0 then
            local cid = room:askToCards(player, {
              min_num = 1,
              max_num = 1,
              skill_name = skill.name,
              cancelable = false,
              pattern = tostring(Exppattern{ id = cids }),
              prompt = "#tea__weibing-choose",
              expand_pile = cids
            })
            if #cid == 1 then
              room:obtainCard(player, cid, true, fk.ReasonPrey)
              table.removeOne(cids, cid[1])
              for _, id in ipairs(cid) do
                room:setCardMark(Fk:getCardById(id), "@@tea__weibing_mark-phase", 0)
              end
            end
          end
        end
      end
      if #cids > 0 then
        room:moveCardTo(cids, Card.DiscardPile, nil, fk.ReasonJustMove, self.name)
        for _, id in ipairs(cids) do
          room:setCardMark(Fk:getCardById(id), "@@tea__weibing_mark-phase", 0)
        end
      end
    end
  end,
}
skill:addEffect(fk.TargetSpecified, weibing)
return skill